package io.gitee.hefren.hhcache.core;

import com.google.common.collect.Maps;
import io.gitee.hefren.hhcache.command.*;

import java.util.Map;
import java.util.Optional;

/**
 * @Date 2024/7/9
 * @Author lifei
 */
public class Commands {
    private static final Map<String, Command> COMMMANDS = Maps.newLinkedHashMap();

    static {
        initCommands();
    }

    private static void initCommands() {
        // 一般命令
        registerCommand(new CommandCommand());
        registerCommand(new PingCommand());
        registerCommand(new InfoCommand());
        registerCommand(new UnknownCommand());
        // string 命令
        registerCommand(new SetCommand());
        registerCommand(new GetCommand());
        registerCommand(new StrlenCommand());
        registerCommand(new ExistsCommand());
        registerCommand(new DelCommand());
        registerCommand(new MsetCommand());
        registerCommand(new MgetCommand());
        registerCommand(new IncrCommand());
        registerCommand(new DecrCommand());

        // list 命令
        registerCommand(new LpushCommand());
        registerCommand(new RpushCommand());
        registerCommand(new LPopCommand());
        registerCommand(new RPopCommand());
        registerCommand(new LlenCommand());
        registerCommand(new LIndexCommand());
        registerCommand(new LrangeCommand());
        // set 命令
        registerCommand(new SaddCommand());
        registerCommand(new SmembersCommand());
        registerCommand(new SismemberCommand());
        registerCommand(new ScardCommand());
        registerCommand(new SremCommand());
        registerCommand(new SpopCommand());
        // hash 命令
        registerCommand(new HsetCommand());
        registerCommand(new HgetCommand());
        registerCommand(new HgetallCommand());
        registerCommand(new HmgetCommand());
        registerCommand(new HlenCommand());
        registerCommand(new HdelCommand());
        registerCommand(new HexistsCommand());
        // zset
        registerCommand(new ZaddCommand());
        registerCommand(new ZcardCommand());
        registerCommand(new ZcountCommand());
        registerCommand(new ZscoreCommand());
        registerCommand(new ZrankCommand());
        registerCommand(new ZremCommand());
    }


    public static Command getCommand(String comName) {
        return Optional.ofNullable(COMMMANDS.get(comName)).orElse(COMMMANDS.get(CommandEnum.UNKNOWN.name()));
    }

    private static void registerCommand(Command com) {
        COMMMANDS.put(com.name(), com);
    }

    public static String[] getCommands() {
        return COMMMANDS.keySet().toArray(new String[0]);
    }
}
